package service

import (
	"context"
	pb "medicine/api/medicine/v1"
	"medicine/internal/biz"
)

// ProductService 商品服务实现，负责处理商品相关的 gRPC 请求
// 调用 usecase 层实现具体业务逻辑
type ProductService struct {
	pb.UnimplementedProductServer                     // gRPC 商品服务基类
	uc                            *biz.ProductUsecase // 商品用例层依赖
}

// NewProductService 创建商品服务实例
func NewProductService(uc *biz.ProductUsecase) *ProductService {
	return &ProductService{uc: uc}
}

// CreateProduct 创建商品
func (s *ProductService) CreateProduct(ctx context.Context, req *pb.CreateProductRequest) (*pb.ProductReply, error) {
	p := &biz.Product{
		Name:        req.Name,
		CategoryID:  req.CategoryId,
		BrandID:     req.BrandId,
		Price:       req.Price,
		Stock:       int(req.Stock),
		Description: req.Description,
	}
	newP, err := s.uc.Create(ctx, p)
	if err != nil {
		return nil, err
	}
	return &pb.ProductReply{
		Id:          newP.ID,
		Name:        newP.Name,
		CategoryId:  newP.CategoryID,
		BrandId:     newP.BrandID,
		Price:       newP.Price,
		Stock:       int32(newP.Stock),
		Description: newP.Description,
	}, nil
}

// GetProduct 获取商品详情
func (s *ProductService) GetProduct(ctx context.Context, req *pb.GetProductRequest) (*pb.ProductReply, error) {
	p, err := s.uc.Get(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	return &pb.ProductReply{
		Id:          p.ID,
		Name:        p.Name,
		CategoryId:  p.CategoryID,
		BrandId:     p.BrandID,
		Price:       p.Price,
		Stock:       int32(p.Stock),
		Description: p.Description,
	}, nil
}

// UpdateProduct 更新商品
func (s *ProductService) UpdateProduct(ctx context.Context, req *pb.UpdateProductRequest) (*pb.ProductReply, error) {
	p := &biz.Product{
		ID:          req.Id,
		Name:        req.Name,
		CategoryID:  req.CategoryId,
		BrandID:     req.BrandId,
		Price:       req.Price,
		Stock:       int(req.Stock),
		Description: req.Description,
	}
	newP, err := s.uc.Update(ctx, p)
	if err != nil {
		return nil, err
	}
	return &pb.ProductReply{
		Id:          newP.ID,
		Name:        newP.Name,
		CategoryId:  newP.CategoryID,
		BrandId:     newP.BrandID,
		Price:       newP.Price,
		Stock:       int32(newP.Stock),
		Description: newP.Description,
	}, nil
}

// DeleteProduct 删除商品
func (s *ProductService) DeleteProduct(ctx context.Context, req *pb.DeleteProductRequest) (*pb.DeleteProductReply, error) {
	err := s.uc.Delete(ctx, req.Id)
	return &pb.DeleteProductReply{Success: err == nil}, err
}

// ListProduct 分页获取商品列表
func (s *ProductService) ListProduct(ctx context.Context, req *pb.ListProductRequest) (*pb.ListProductReply, error) {
	products, total, err := s.uc.List(ctx, int(req.Page), int(req.PageSize))
	if err != nil {
		return nil, err
	}
	var replies []*pb.ProductReply
	for _, p := range products {
		replies = append(replies, &pb.ProductReply{
			Id:          p.ID,
			Name:        p.Name,
			CategoryId:  p.CategoryID,
			BrandId:     p.BrandID,
			Price:       p.Price,
			Stock:       int32(p.Stock),
			Description: p.Description,
		})
	}
	return &pb.ListProductReply{Products: replies, Total: int32(total)}, nil
}
